package com.qixiucoding.ann;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.util.Map;

/**
 * @Description TODO
 * @Author longhj
 * @Date 2022/03/05 星期六 12:49
 */
public abstract class IDealBuss {
    public final static String STREMPTY = "";

    public <K, V> void dobBuss(String field, JSONObject dealData, Map<K, V> share) {
        init(field, dealData, share);
        try {
            dealBuss(field, dealData, share);
        } catch (Exception e) {
            e.printStackTrace();
        }
        destroy(field, dealData, share);
    }

    public <K, V> void init(String field, JSONObject dealData, Map<K, V> share){
        if (StrUtil.isBlank(dealData.getString(field))) {
            destroy(field, dealData, share);
            return;
        }
        try {
            this.dealBuss(field, dealData, share);
        } catch (Exception e) {
            destroy(field, dealData, share);
            e.printStackTrace();
        }
    }


    protected abstract <K, V> void dealBuss(String field, JSONObject dealData, Map<K, V> share) throws Exception;


    public <K, V> void destroy(String field, JSONObject dealData, Map<K, V> share) {
        if (StrUtil.isBlank(dealData.getString(field))) {
            dealData.put(field, STREMPTY);
        }
    }
}
